/*
 * Copyright 2002-2006 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.tigris.jasmincc.configuration.implementation;

import java.io.*;
import java.util.*;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.tigris.jasmincc.configuration.ConfigurationManager;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

/**
 * Houses conifugration data.
 * 
 * @author Sam Huang
 * 
 */
public class SimpleConfigurationManager extends DefaultHandler implements
		ConfigurationManager {

	protected String name;

	protected String buffer;

	protected List<String> buildTasks = null;

	protected Properties options = null;

	protected Properties tokens = null;

	protected List<String> skips = null;

	protected String defaultGrammar;

	public SimpleConfigurationManager() {
		super();
	}

	public void init(String fileName) throws Exception {

		buildTasks = new ArrayList<String>();
		options = new Properties();
		tokens = new Properties();
		skips = new ArrayList<String>();

		SAXParserFactory spf = SAXParserFactory.newInstance();
		SAXParser sp = spf.newSAXParser();

		// parse the file and also register this class for call backs
		sp.parse(fileName, this);
	}

	@Override
	public void characters(char[] arg0, int arg1, int arg2) throws SAXException {
		// TODO Auto-generated method stub
		super.characters(arg0, arg1, arg2);
		buffer = new String(arg0, arg1, arg2);
	}

	@Override
	public void endElement(String arg0, String arg1, String arg2)
			throws SAXException {
		// TODO Auto-generated method stub
		super.endElement(arg0, arg1, arg2);
		if (arg2.equals("default")) {
			defaultGrammar = buffer;
		} else if (arg2.equals("name")) {
			this.name = buffer;
		} else if (arg2.equals("build")) {
			this.buildTasks.add(buffer);
		} else if (arg2.equals("skip")) {
			this.skips.add(buffer);
		}
	}

	/**
	 * @return the buildTasks
	 */
	public List<String> getBuilds() {
		return buildTasks;
	}

	public String getDefaultGrammarBase() {
		return defaultGrammar;
	}

	/**
	 * @return the name
	 */
	public String getName() {
		return name;
	}

	/**
	 * @return the grammarBases
	 */
	public Properties getOptions() {
		return options;
	}

	public String getParser() {
		// TODO Auto-generated method stub
		return null;
	}

	public String getParserClassName() {
		// TODO Auto-generated method stub
		return null;
	}

	public List<String> getSkips() {
		return skips;
	}

	public Properties getTokens() {
		return tokens;
	}

	public void importOptions(String optionsFileName) throws Exception {
		File file = new File(optionsFileName);
		if (file.exists() == false) {
			throw new FileNotFoundException(optionsFileName);
		}
		Properties options = new Properties();
		options.load(new FileInputStream(file));
		this.options.putAll(options);
	}

	/**
	 * Import predefined skipping tokens.
	 * 
	 * @param skipsFileName
	 * @throws Exception
	 */
	public void importSkips(String skipsFileName) throws Exception {
		File file = new File(skipsFileName);
		if (file.exists() == false) {
			throw new FileNotFoundException(skipsFileName);
		}
		BufferedReader reader = new BufferedReader(new FileReader(file));
		String line;
		while ((line = reader.readLine()) != null) {
			if (!skips.contains(line)) {
				skips.add(line);
			}
		}
	}

	public void importTokens(String tokensFileName) throws Exception {
		File file = new File(tokensFileName);
		if (file.exists() == false) {
			throw new FileNotFoundException(tokensFileName);
		}
		Properties tokens = new Properties();
		tokens.load(new FileInputStream(file));
		this.tokens.putAll(tokens);
	}

	@Override
	public void startElement(String arg0, String arg1, String arg2,
			Attributes arg3) throws SAXException {
		// TODO Auto-generated method stub
		super.startElement(arg0, arg1, arg2, arg3);
		if (arg2.equals("option")) {
			options.put(arg3.getValue("name"), arg3.getValue("value"));
		} else if (arg2.equals("token")) {
			tokens.put(arg3.getValue("name"), arg3.getValue("value"));
		} else if (arg2.equals("import")) {
			try {
				String tokens = arg3.getValue("tokens");
				if (tokens != null) {
					importTokens(tokens);
				}
				String skips = arg3.getValue("skips");
				if (skips != null) {
					importSkips(skips);
				}
				String options = arg3.getValue("options");
				if (options != null) {
					importOptions(options);
				}
			} catch (Exception ex) {
				ex.printStackTrace();
			}
		}
	}
}
